package com.jvtd.eyl.data;

import android.content.Context;

import com.jvtd.bean.EmptyBean;
import com.jvtd.eyl.bean.binding.BookBean;
import com.jvtd.eyl.bean.binding.BrowseBean;
import com.jvtd.eyl.bean.binding.ClassBean;
import com.jvtd.eyl.bean.binding.PeriodicalBean;
import com.jvtd.eyl.bean.db.User;
import com.jvtd.eyl.bean.http.BannerBean;
import com.jvtd.eyl.bean.http.BaseResponse;
import com.jvtd.eyl.bean.http.LoginReqBean;
import com.jvtd.eyl.bean.http.LoginResBean;
import com.jvtd.eyl.bean.http.MyBookBean;
import com.jvtd.eyl.bean.http.TimeBean;
import com.jvtd.eyl.data.api.ApiHelper;
import com.jvtd.eyl.data.db.DbHelper;
import com.jvtd.eyl.data.sp.PreferencesHelper;
import com.jvtd.eyl.di.ApplicationContext;

import java.util.List;

import javax.inject.Inject;
import javax.inject.Singleton;

import io.reactivex.Observable;

/**
 * Created by Administrator on 2017/10/16.
 * 数据管理实现类
 */
@Singleton
public class AppDbManager implements DbManager
{
  private Context mContext;
  private DbHelper mDbHelper;
  private ApiHelper mApiHelper;
  private PreferencesHelper mPreferencesHelper;

  @Inject
  public AppDbManager(@ApplicationContext Context context, DbHelper dbHelper,
                      ApiHelper apiHelper, PreferencesHelper preferencesHelper)
  {
    mContext = context;
    mDbHelper = dbHelper;
    mApiHelper = apiHelper;
    mPreferencesHelper = preferencesHelper;
  }

  @Override
  public boolean isLogin()
  {
    return mPreferencesHelper.isLogin();
  }

  @Override
  public void setLogin()
  {
    mPreferencesHelper.setLogin();
  }

  @Override
  public void clearLogin()
  {
    mPreferencesHelper.clearLogin();
  }

  @Override
  public boolean isStartGuidePager()
  {
    return mPreferencesHelper.isStartGuidePager();
  }

  @Override
  public void saveGuidePageState()
  {
    mPreferencesHelper.saveGuidePageState();
  }



  @Override
  public Observable<Boolean> saveCurrentUser(User user)
  {
    return mDbHelper.saveCurrentUser(user);
  }

  @Override
  public Observable<Integer> clearCurrentUser()
  {
    return mDbHelper.clearCurrentUser();
  }

  @Override
  public Observable<User> getCurrentUser()
  {
    return mDbHelper.getCurrentUser();
  }

  @Override
  public void setLoginOut()
  {
    this.clearCurrentUser().subscribe();
    this.clearLogin();
  }

  @Override
  public Context context()
  {
    return mContext;
  }

  @Override
  public Observable<LoginResBean> login(LoginReqBean loginReqBean) {
    return mApiHelper.login(loginReqBean);
  }

  @Override
  public Observable<BaseResponse<List<BannerBean>>> getBannerList() {
    return mApiHelper.getBannerList();
  }

  @Override
  public Observable<BaseResponse<List<ClassBean>>> getClassList() {
    return mApiHelper.getClassList();
  }

  @Override
  public Observable<BaseResponse<List<PeriodicalBean>>> getAllPeriodicalList() {
    return mApiHelper.getAllPeriodicalList();
  }

  @Override
  public Observable<BaseResponse<List<ClassBean>>> getSecondaryClassList(String id) {
    return mApiHelper.getSecondaryClassList(id);
  }

  @Override
  public Observable<BaseResponse<List<PeriodicalBean>>> getClassPeriodicalList(String category_id) {
    return mApiHelper.getClassPeriodicalList(category_id);
  }

  @Override
  public Observable<BaseResponse<List<TimeBean>>> getTimeClassList(String document_id) {
    return mApiHelper.getTimeClassList(document_id);
  }

  @Override
  public Observable<BaseResponse<List<BookBean>>> getBookList(String document_id, String user_id, String sorts, String year) {
    return mApiHelper.getBookList(document_id,user_id,sorts,year);
  }

  @Override
  public Observable<BaseResponse<EmptyBean>> addBook(String user_id, String periodical_id) {
    return mApiHelper.addBook(user_id,periodical_id);
  }

  @Override
  public Observable<BaseResponse<EmptyBean>> delBook(String user_id, String periodical_id) {
    return mApiHelper.delBook(user_id,periodical_id);
  }

  @Override
  public Observable<BaseResponse<EmptyBean>> addBrowse(String user_id, String periodical_id) {
    return mApiHelper.addBrowse(user_id,periodical_id);
  }

  @Override
  public Observable<BaseResponse<List<BrowseBean>>> getMyBrowseList(String user_id) {
    return mApiHelper.getMyBrowseList(user_id);
  }

  @Override
  public Observable<BaseResponse<List<PeriodicalBean>>> searchPeriodicalList(String category_name) {
    return mApiHelper.searchPeriodicalList(category_name);
  }

  @Override
  public Observable<BaseResponse<List<MyBookBean>>> getMyBookList(String user_id) {
    return mApiHelper.getMyBookList(user_id);
  }
}